package com.chinacache.maint.webssh.ldap;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

import org.jdom.Attribute;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;

@SuppressWarnings("unchecked")
public class UMParas {

	private static HashMap prop;

	private static long lastLoadTime;

	private static long interval = 0x186a0L; // refresh per 100 second

	// static Class class$0; /* synthetic field */

	public UMParas() {
	}

	// input an para and return the result
	public static synchronized String getPara(String paras)
			throws IllegalArgumentException {
		if (paras == null || paras.trim().length() == 0)
			throw new IllegalArgumentException("Parameter's value invalid.");
		long currentTime = System.currentTimeMillis();
		if (prop == null || currentTime - lastLoadTime > interval)
			reloadDom();
		Object obj = prop.get(paras);
		if (obj != null)
			return (String) obj;
		else
			return null;
	}

	// load the xml file
	private static synchronized void reloadDom() {
		if (prop == null)
			prop = new HashMap();
		SAXBuilder builder = new SAXBuilder();
		Document read_doc = null;
		try {
			read_doc = builder.build(UMParas.class
					.getResource("ldapconfig.xml"));
		} catch (JDOMException e) {
			e.printStackTrace();
		}
		Element rootElement = read_doc.getRootElement();
		List list = rootElement.getChildren("para");
		for (Iterator i = list.iterator(); i.hasNext();) {
			Element current = (Element) i.next();
			List item = current.getChildren("item");
			Attribute code;
			Attribute value;
			for (Iterator j = item.iterator(); j.hasNext(); prop.put(code
					.getValue(), value.getValue())) {
				Element init = (Element) j.next();
				code = init.getAttribute("code");
				value = init.getAttribute("value");
			}

		}
		System.out.println("load sucess");
		lastLoadTime = System.currentTimeMillis();
	}

	public static void main(String args[]) {
		System.out.println(getPara("hostname"));
	}

}